package com.springboot.erp.service.impl;


import com.springboot.erp.entity.Outbound;
import com.springboot.erp.mapper.OutboundMapper;
import com.springboot.erp.service.OutboundService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
/**
 * 出库服务实现类
 */
public class OutboundServiceImpl implements OutboundService {

    @Resource
    private OutboundMapper outboundMapper;

    /**
     * 创建出库单
     */
    @Override
    public boolean createOutbound(Outbound outbound) {
        // 插入出库单
        int outboundResult = outboundMapper.insertOutbound(outbound);
        if (outboundResult > 0) {
            // 插入出库单明细
            int itemResult = outboundMapper.insertOutboundItem(outbound);
            return itemResult > 0;
        }
        return false;
    }

    /**
     * 查询出库单
     */
    @Override
    public Outbound selectOutboundById(Long id) {
        return outboundMapper.selectOutboundList(id);
    }

    /**
     * 修改出库单
     */
    @Override
    public boolean updateOutbound(Outbound outbound) {
        // 更新出库单
        int outboundResult = outboundMapper.updateOutboundById(outbound);
        if (outboundResult > 0) {
            // 更新出库单明细
            int itemResult = outboundMapper.updateOutboundItem(outbound);
            return itemResult > 0;
        }
        return false;
    }

    /**
     * 删除出库单
     */
    @Override
    public boolean deleteOutbound(Long id) {
        // 先删除子表数据
        int itemResult = outboundMapper.deleteOutboundItemByHeaderId(id);
        // 再删除主表数据
        int outboundResult = outboundMapper.deleteOutboundById(id);
        return outboundResult > 0;
    }
}